﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
//using System.ServiceModel.Web;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Xml.Linq;


namespace LogViewApp.Web
{
    public class Service1 : IService1
    {
        static XElement xStatus = new XElement("Status");
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public List<string> GetCloudBlobList()
        {
            IEnumerable<IListBlobItem> Listblobs;
            CloudBlobContainer container;
            CloudBlobClient blobs;
            CloudStorageAccount account;
            account = Microsoft.WindowsAzure.CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            blobs = account.CreateCloudBlobClient();
            var ContainerName = string.Format("{0}{1}{2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            container = blobs.GetContainerReference(ContainerName);
            SharedAccessPolicy readPolicy;
            readPolicy = new SharedAccessPolicy()
            {
                Permissions = SharedAccessPermissions.Read,
                SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(10)
            };
            Listblobs = container.ListBlobs(new BlobRequestOptions() { UseFlatBlobListing = true });
            var UriStringList = from CloudBlob blob in Listblobs
                                select (blob.Uri.AbsoluteUri + blob.GetSharedAccessSignature(readPolicy));
            return UriStringList.ToList<string>();
        }

        
        public string GetStatusUpdate()
        {
            // Add your operation implementation here
            //return DateTime.Now.ToLongTimeString();
            AddStatus(new XElement("Time", DateTime.Now.ToLongTimeString()));
            XElement xRet = xStatus;
            ResetStatus();
            return xRet.ToString(SaveOptions.DisableFormatting);
        }

        static public void ResetStatus()
        {
            xStatus = new XElement("Status");
        }

        static public void AddStatus(XElement newone)
        {
            xStatus.Add(newone);
        }



    }
}
